package co.javatoday.springdata.redis.service;

import java.io.Serializable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service("redisUtils")
public class RedisUtils {
	@Autowired
	RedisTemplate<String, String> redisTemplate;

	/**
	 * 向redis里面添加key-value格式的数据
	 * 
	 * @param key
	 *            key
	 * @param value
	 *            value
	 */

	public void set(final String key, final String value, final long exp) {
		redisTemplate.execute(new RedisCallback<Object>() {
			@Override
			public Object doInRedis(RedisConnection connection) throws DataAccessException {
				byte[] key_ = key.getBytes();
				byte[] value_ = value.getBytes();
				connection.set(key_, value_);
				connection.expire(key_, exp);
				return true;
			}
		});
	}

	public void set(String key, String value) {
		this.set(key, value, 600);
	}

	public void updateValue(String key, long exp) {
		String value = (String) this.get(key);
		this.set(key, value, exp);
	}

	/**
	 * 根据key从redis里面取出value
	 * 
	 * @param key
	 *            key
	 */
	public Serializable get(final String key) {

		return redisTemplate.execute(new RedisCallback<Serializable>() {
			@Override
			public Serializable doInRedis(RedisConnection connection) throws DataAccessException {

				byte[] keyBytes = key.getBytes();
				byte[] bytes = connection.get(keyBytes);
				String str;
				if (bytes == null) {
					str = "";
				} else {
					str = new String(bytes);
				}
				return str;
			}
		});
	}

	public Serializable isEmpty(final String key) {
		return redisTemplate.execute(new RedisCallback<Serializable>() {

			@Override
			public Serializable doInRedis(RedisConnection connection) throws DataAccessException {
				// TODO Auto-generated method stub
				return connection.exists(key.getBytes());
			}
		});
	}

	public Serializable del(final String key) {
		return redisTemplate.execute(new RedisCallback<Serializable>() {

			@Override
			public Serializable doInRedis(RedisConnection connection) throws DataAccessException {
				// TODO Auto-generated method stub
				if (isEmpty(key) == null) {
					return false;
				} else {
					connection.del(key.getBytes());
				}
				return true;
			}
		});

	}

	public Serializable showTime(final String key){
		return redisTemplate.execute(new RedisCallback<Serializable>() {

			@Override
			public Serializable doInRedis(RedisConnection connection) throws DataAccessException {
				// TODO Auto-generated method stub
				if (connection.exists(key.getBytes())) {
					return connection.ttl(key.getBytes());
				}
				return null;
			}
		});
	}
	/*
	 * add some new method
	 * 
	 * 
	 * */
	public Serializable updateExp(final String key, final long exp){
		return redisTemplate.execute(new RedisCallback<Serializable>() {

			@Override
			public Serializable doInRedis(RedisConnection connection) throws DataAccessException {
				// TODO Auto-generated method stub
				if (connection.exists(key.getBytes())) {
					connection.expire(key.getBytes(), exp);
					return true;
				}
				return false;
			}
		});
	}
	public Serializable reSetKey(final String old_key, final String new_key){
		return redisTemplate.execute(new RedisCallback<Serializable>() {

			@Override
			public Serializable doInRedis(RedisConnection connection) throws DataAccessException {
				// TODO Auto-generated method stub
				connection.rename(old_key.getBytes(), new_key.getBytes());
				return null;
			}
		});
	}
}
